﻿var columns =
            [
                 { display: 'Key', name: 'key', type: 'text', hide: 1 },
                 { display: 'isParent', name: 'isParent', type: 'text', hide: 1 },
                 //{ display: 'ParentId', name: 'ParentId', type: 'text', hide: 1 },
                 {display: '类别名称', name: 'ParentName', width: 100, hide: 1 },
                { display: '菜单编码', name: 'Code', width: 100 },
                { display: '菜单名称', name: 'Name', width: 120 },
                { display: '是否子菜单', name: 'isChild', width: 80 },
                { display: '匹配资源', name: 'MacthPermission', width: 120 },
                { display: '菜单图标', name: 'IconUrl', width: 160 },
                { display: '菜单描述', name: 'Des', width: 200 },
                { display: '排序数字', name: 'OrderNum', width: 80 }
             ];

//init controls
var manager;
$(document).ready(function () {
    initligerGrid();
});
function initligerGrid() {
    manager = null;
    manager = $("#operationGrid").ligerGrid({
        columns: columns,
        title: '菜单类别',
        showTitle: true,
        width: '100%',
        enabledSort: true,
        height: '99%',
        rownumbers: true,
        checkbox: false,
        toolbar: { items: [
                            { text: '增加根菜单', click: addOperation, icon: 'add' },
                            { line: true },
                             { text: '增加子菜单', click: addChildOperation, icon: 'add' },
                            { line: true },
                            { text: '修改', click: modifyOperation, icon: 'modify' },
                            { line: true },
                             { text: '与资源对应', click: authorizate, img: '../Content/images/table_relationship.png' },
                            { line: true },
                            { text: '删除', click: deleteOperation, img: '../Content/images/delete.gif' }
                           ]
        },
        groupColumnName: 'ParentName', groupColumnDisplay: ' 根菜单 ',
        url: "../Operation/GetJsonOfOperations?rand="+Math.random()
    });
}
//add root
function addOperation() {

    $.ligerDialog.open({ url: '../Operation/Add', height: getDialogHeight(), width: getDialogWidth(), isResize: true, isHidden: false
    });

    //    var returnValue = window.showModalDialog('../Operation/Add', window, "help:No;resizable:No;status:No;scroll:yes;dialogWidth=500px;dialogHeight=400px");
}
//add Children
function addChildOperation() {
    var row = manager.getSelectedRow();
    if (!row) { $.ligerDialog.success('请选择根菜单行！'); return; }
    var tipInfo = row["isParent"].toString() == "1" ? "不能给子菜单再添加子菜单!" : "";
    if (tipInfo.length > 0) { $.ligerDialog.success(tipInfo); return; }
    $.ligerDialog.open({ url: '../Operation/Add?operationId=' + row["Key"].toString() , height: getDialogHeight(), width: getDialogWidth(),  isResize: true, isHidden: false
    });

    //    var returnValue = window.showModalDialog('../Operation/Add', window, "help:No;resizable:No;status:No;scroll:yes;dialogWidth=500px;dialogHeight=400px");
}
//delete
function deleteOperation() {
    var row = manager.getSelectedRow();
    if (!row) { $.ligerDialog.success('请选择需要删除行！'); return; }
    var tipInfo = row["isParent"].toString() == "1" ? "确定要删除吗？" : "确定要删除吗？会同时删除所有子菜单！";
    if (tipInfo.length > 0) { $.ligerDialog.success(tipInfo); return; }
    $.ligerDialog.confirm(tipInfo, function (yes) {
        if (yes) {
            $.get("../Operation/Delete?operationId=" + row["Key"].toString(), {}, function (data) {
                if (data != undefined && data == "ok") {
                    initligerGrid();
                }
            });
        }
    });
}
//update 
function modifyOperation() {
    var row = manager.getSelectedRow();
    if (!row) {
        $.ligerDialog.success('请选择要编辑的行!'); return;
    }
    $.ligerDialog.open({ url: '../Operation/Edit?operationId=' + row["Key"].toString() + "&type=" + row["isParent"].toString(), height: getDialogHeight(), width: getDialogWidth(), isResize: true, isHidden: false
    });
}


//
//给菜单赋权限(一对一)
function authorizate() {
    var row = manager.getSelectedRow();
    if (!row) {
        $.ligerDialog.success('请选择要赋权的行!'); return;
    }
    $.ligerDialog.open({ url: '../Permission/SelectPermission?operationId=' + row["Key"].toString() + "&rand=" + Math.random(), height: getDialogHeight(), width: getDialogWidth(),
     isResize: true, isHidden: false
    });
}

